var $ = require('jquery');

var common = {
  /**
   * 获取验证码
   * @param  {} id
   */
  getCode: function(id){
    var oBtn = $(id);
    oBtn.click(function(){
      var tel = $("#tel").val();
      if( !mobile.test(tel) ){
          alert("请正确输入您的手机号码！");
          return false;
      }else{
          $.ajax({
              url: "http://s1.bd3.hengqiedu.cn/promotion/message/sendCode",
              type:'POST',
              data:"phone="+tel,
              dataType:'json',
              success:function(data){
                  if(data.code == 0){
                      alert("验证码已发送至您的手机，请注意查收");
                  }else {
                      alert(data.msg);
                  }
              }
          })
      }
      return false;
    })
  },
  submitResults: function(id){
    var oBtn = $(id);
    oBtn.click(function(){
      var check = setAct();
      if( !check ) return;
      var phoneNumber=$(".sc-tel").val();
      var scyzm=$(".sc-yzm").val();
      var property=$("#cs_property").val();
      var url_kaiba=window.location.href;
      
      if(phoneNumber=="" || !mobile.test(phoneNumber) ){
          alert("请正确输入您的手机号码！");
          return false;
      }
      if(scyzm=="" || scyzm==null){
          alert("验证码不能为空！");
          return false;
      }
      
      if(url_kaiba.indexOf('kaiba')>0){
          var kaiba_data=url_kaiba.split('?kaiba=')[1];
      }
      var dataArr ={
        answer1: $("input[name=answer1]:checked").val(),
        answer2: $("input[name=answer2]:checked").val(),
        answer3: $("input[name=answer3]:checked").val(),
        answer4: $("input[name=answer4]:checked").val(),
        answer5: $("input[name=answer5]:checked").val(),		
        tel:phoneNumber,
        code:scyzm,
        thisUrl:window.location.href,
        preUrl:document.referrer,
        kaibaUrl:kaiba_data,
        'property':property,
        hslx:'wap:女生学什么好—适合学会计吗?',
        objective:"女生学会计",
        catname:""
      }
      
      $.ajax({
          url: "http://s1.bd3.hengqiedu.cn/promotion/guestbook/kTest",
          type:'POST',
          dataType:'json',
          data:dataArr,
          success:function(data){
            if(data.code == 0){
              $(".sc-tel").val("");
              alert("测试成功！");
              $("#showtest").text(data.msg);
              $("#showtestP").show();
            }
            else{
              alert(data.msg);
              return false;
            }
          }
      })
    })
  },
  
  /*上海自考推广页面zk-shanghai-html 表单提交*/
  submitResultsSh: function(id){
    var oBtn = $(id);
    oBtn.click(function(){
      var type = $(this).attr("val");
      if($("#name" + type).val() == "" || $('#city'+type).val()== "" ){
          alert("请完善信息！");
          return ;
      };
      if($("#tel" + type ).val() == "" || !/^1[3,4,5,8,9,6,7]\d{9}$/.test($("#tel" + type).val()) ){
          alert("请正确输入您的手机号码！");
          return ;
      }
      var params ={
          name:$("#name" + type).val(),
          tel:$("#tel" + type).val(),
          //trueName:$('#city'+type).val(),
          trueName: '未分配校区',
          property:0,
          type:type,
          typeid:998,
          siteid:1,
          isPhone:1,
          hslx:'wap:华南自考留言页',
          objective:'在线报名',
          thisUrl:window.location.href,
          preUrl:document.referrer,
          property:2
      }
      $.ajax({
          url: "/promotion/guestbook/zkBaoming",
          type:'POST',
          data:params,
          success:function(data){
              data = JSON.parse(data);
              if(data.code=='0' ){
                  alert("提交成功，稍后会有老师联系您确保您已收到资料并为您解答常见问题，请注意接听");
              }else {
                  alert(data.msg);
              }
          }
      })
    }) 
  }
}

module.exports = common;
